<template>
    <!--组件的配置需要去仔细看官网组件代码示例-->
    <div class="heightAll widthAll" style="overflow: hidden;">
        <el-form :model="bookInfo" :rules="rules" auto label-position="left" label-width="auto" ref="bookInfo"
                 style="margin: 10px; width: 800px; ">
            <el-form-item label="书名" prop="name">
                <el-input placeholder="请输入书名" size="medium"
                          v-model="bookInfo.name">
                    <template slot="append"><i class="el-icon-notebook-1"></i></template>
                </el-input>
            </el-form-item>
            <el-form-item label="作者" prop="author">
                <el-input placeholder="请输入作者" size="medium"
                          v-model="bookInfo.author">
                    <template slot="append"><i class="el-icon-edit-outline"></i></template>
                </el-input>
            </el-form-item>
            <el-form-item label="分类" prop="category">
                <el-select clearable placeholder="请选择书籍分类"
                           v-model="bookInfo.category">
                    <el-option
                            :key="category.id"
                            :label="category.name"
                            :value="category.id"
                            v-for="category in getCategories">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="库存" prop="count">
                <el-input-number :min="0" label="库存" v-model="bookInfo.count"></el-input-number>
            </el-form-item>
            <el-form-item>
                <!--见el-form ref属性-->
                <el-button :disabled="disabled" @click="submitForm('bookInfo')" type="primary">添加图书</el-button>
                <el-button @click="resetForm('bookInfo')">重置</el-button>
            </el-form-item>
        </el-form>
        <div></div>
    </div>
</template>

<script>
import bookInfo from "../../store/bookInfo";
import categoryInfo from "../../store/categoryInfo";
import common from "../../store/common";

export default {
    data() {
        return {
            //深拷贝，否则会间接修改store
            bookInfo: JSON.parse(JSON.stringify(bookInfo.getters.getBookForm)),
            rules: bookInfo.getters.getFormRules,
            disabled: false, /*服务器回应没到时禁用按钮*/
        };
    },
    methods: {
        submitForm(formName) {
            const _this = this;
            _this.$refs[formName].validate((valid) => {
                //表单规则通过
                if (valid) {
                    _this.disabled = true;
                    /*我草，又因为没写this.bookinfo搞混了*/
                    bookInfo.dispatch('checkBookExist', _this.bookInfo).then((res) => {
                        _this.disabled = false;
                        if (res) //如果存在
                            _this.showExistBookWarn();
                        else {
                            bookInfo.dispatch('addBook', _this.bookInfo).then((rs) => {
                                common.afterUpdateActions(_this, rs, '添加', bookInfo.state.routeUrl.bookList, true);
                            });
                        }
                    });
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.bookInfo.count = 0; //计数器不能复位，可能要设置slot，懒得弄
        },
        showExistBookWarn() {
            this.$message({type: 'warning', message: '该书名已存在'});
        },
    }, mounted() {
        categoryInfo.dispatch('queryCategoriesAll');
    }, computed: {
        getCategories: function () {
            return categoryInfo.getters.getCategories;
        }
    }
}
</script>

<style scoped>
</style>